﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using MVCPets.Data;
using MVCPets.Data.Model;

namespace MVCPets.Models
{
  public class RoleModel
  {
    #region Public Properties
    public int? ID { get; set; }
    [DisplayName("Role Name")]
    public string RoleName { get; set; }
    [DisplayName("Role Value")]
    public int RoleValue { get; set; }
    #endregion

		private static IList<Role> GetRoleList()
		{
			return Data.DataAccess.DataStore.LoadEntireTable<Role>();
		}
    #region Constructors
    public RoleModel()
    {

    }

    public RoleModel(int id)
    {
			Role role = Data.DataAccess.DataStore.LoadObject<Role>(id);
			this.ID = role.ID;
			this.RoleName = role.RoleName;
			this.RoleValue = role.RoleValue;
    }    
    #endregion    

    #region Static Methods
    public static List<RoleModel> GetRoles()
    {
      List<RoleModel> ret = new List<RoleModel>();
			foreach(var r in GetRoleList())
				ret.Add(new RoleModel() 
				{
          ID = r.ID,
          RoleName = r.RoleName,
          RoleValue = r.RoleValue
        });

      return ret;
    }

		public static Role GetRole(int id)
		{
			return GetRoleList().Where(R => R.ID == id).FirstOrDefault();
		}
    #endregion

    #region Public Methods
    public bool Save()
    {
      bool ret = false;

      if (this.ID.HasValue) ret = Update();
      else ret = Insert();

      return ret;
    }

    public bool Delete()
    {
			return Data.DataAccess.DataStore.DeleteObject<Role>(this.ID);
    }
    #endregion

    #region Private Methods
    private bool Insert()
    {
			return Data.DataAccess.DataStore.InsertObject(new Role()
			{
				RoleName = this.RoleName,
				RoleValue = this.RoleValue
			});
    }

    private bool Update()
    {
			return Data.DataAccess.DataStore.UpdateObject(new Role()
				{
					RoleName = this.RoleName,
					RoleValue = this.RoleValue,
					ID = this.ID.Value
				});
    }
    #endregion
  }
}